package com.ld.admin.controller.system;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SysSensitiveWordModel;
import com.ld.admin.service.system.SysSensitiveWordSetService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;
import com.ld.shieldsb.sensitiveword.service.SensitiveWordServiceType;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 敏感词设置服务
 * 
 * @ClassName SysSensitiveWordController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年05月29日 16:31:33
 *
 */
@Api(tags = "敏感词设置")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "sensitiveword.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysSensitiveWordSetController")
@RequestMapping("${shieldsb.adminPath}/system/sensitivewordset")
public class SysSensitiveWordSetController extends AdminBasicController<SysSensitiveWordModel> {
    @Autowired
    private SysSensitiveWordSetService service;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "a/system/sensitivewordset/syssensitiveword_main";
    }

    // ===================== 设置 begin =======================
    @ApiOperation(value = "获取设置信息", notes = "")
    @RequestMapping(path = "/getSetInfo", method = RequestMethod.GET)
    public String getSetInfo() {
        service.getSetInfo(request);
        return "a/system/sensitivewordset/set/syssensitiveword_setinfo";
    }

    @ApiOperation(value = "重新加载敏感词", notes = "")
    @RequestMapping(path = "/reload", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object reload() {
        Result result = service.reloadDicts();
        return Web.Response.result(result);
    }

    @ApiOperation(value = "重新加载服务", notes = "")
    @RequestMapping(path = "/reloadService", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object reloadService() {
        Result result = service.reloadService();
        return Web.Response.result(result);
    }

    @ApiOperation(value = "跳转到更新页面", notes = "")
    @RequestMapping(path = "/toPropertiesUpd", method = RequestMethod.GET)
    public String toPropertiesUpd() {
        setAttributes();
        String key = getString("key");
        service.getProperties(request, key);
        return "a/system/sensitivewordset/set/syssensitiveword_update";
    }

    @ApiOperation(value = "更新配置信息", notes = "")
    @RequestMapping(path = "/propertiesUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object propertiesUpdate(String... args) throws ServletException, IOException {
        Result result = service.updateProperties();
        return Web.Response.result(result);
    }

    @ApiOperation(value = "创建配置文件", notes = "")
    @RequestMapping(path = "/createconfig", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object createConfig() {
        Result result = service.createConfigFile();
        return Web.Response.result(result);
    }

    @ApiOperation(value = "检查表是否存在", notes = "")
    @RequestMapping(path = "/checkTable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object checkTable() {
        Result result = service.checkTable();
        return Web.Response.result(result);
    }

    @ApiOperation(value = "创建表", notes = "")
    @RequestMapping(path = "/createTable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object createTable() {
        Result result = service.createTable();
        return Web.Response.result(result);
    }

    // ===================== 设置 end =======================
    // ===================== file敏感词管理 begin =======================
    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        service.getDictionarys(request);
        String serviceType = service.getServiceType();
        if (SensitiveWordServiceType.DATABASE.getValue().equals(serviceType)) { // 数据库
            return "a/system/sensitivewordset/manage_db/syssensitiveword_search";
        }
        return "a/system/sensitivewordset/manage/syssensitiveword_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = service.getSearchQueryModel(request);
        service.getPageBean(request, getModelClass(), queryModel);
        service.getDictionaryItemPageBean(request);
        return "a/system/sensitivewordset/manage/syssensitiveword_list";
    }

    @ApiOperation(value = "敏感词展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/sensitivewordset/manage/syssensitiveword_show";
    }

    @ApiOperation(value = "跳转到添加/修改敏感词", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        service.getDictionarys(request);
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/sensitivewordset/manage/syssensitiveword_update";
    }

    @ApiOperation(value = "添加/修改敏感词", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(SysSensitiveWordModel model) {
        Result result = service.addOrUpdateDictItem(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除敏感词", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        Result result = service.delDictItem();
        return Web.Response.result(result);
    }

    // ===================== file敏感词管理 end =======================
    // ===================== 数据库敏感词管理 begin =======================
    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "db/toSearch", method = RequestMethod.GET)
    public String toSearchDB() {
        return "a/system/sensitivewordset/manage_db/syssensitiveword_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "db/search", method = RequestMethod.POST)
    public String searchDB() {
        QueryModel queryModel = service.getSearchQueryModel(request);
        service.getPageBean(request, getModelClass(), queryModel);
        return "a/system/sensitivewordset/manage_db/syssensitiveword_list";
    }

    @ApiOperation(value = "敏感词展示", notes = "")
    @RequestMapping(path = "db/show", method = RequestMethod.GET)
    public String showDB() {
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/sensitivewordset/manage_db/syssensitiveword_show";
    }

    @ApiOperation(value = "跳转到添加/修改敏感词", notes = "")
    @RequestMapping(path = "db/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpdDB() {
        long id = getLong("id");
        service.getOne(request, getModelClass(), id);
        return "a/system/sensitivewordset/manage_db/syssensitiveword_update";
    }

    @ApiOperation(value = "添加/修改敏感词", notes = "")
    @RequestMapping(path = "db/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpdDB(@Valid SysSensitiveWordModel model) {
        Result result = service.addOrUpdDictItem(model);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除敏感词", notes = "")
    @RequestMapping(path = "db/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object delDB() {
        String id = getString("id");
        Result result = service.delDictItem(id);
        return Web.Response.result(result);
    }
    // ===================== 数据库敏感词 end =======================
    // ===================== 测试 begin =======================

    @ApiOperation(value = "跳转到敏感词测试页面", notes = "")
    @RequestMapping(path = "/toTest", method = RequestMethod.GET)
    public String toTest() {
        return "a/system/sensitivewordset/test/syssensitiveword_test";
    }

    @ApiOperation(value = "敏感词测试", notes = "")
    @RequestMapping(path = "/test", method = RequestMethod.POST)
    public Object test() {
        service.check(request);
        return "a/system/sensitivewordset/test/syssensitiveword_testResult";
    }
    // ===================== 测试 end =======================
}